<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
     <el-button v-permission="['GET /admin/alibaba/listCybUserGroup']" class="filter-item" type="primary" icon="el-icon-edit" @click="syncProductLibrary">同步品类库</el-button>
      <!-- <el-input v-model="listQuery.libraryid" clearable class="filter-item" style="width: 200px;" placeholder="类库ID"/> -->
      <el-button v-permission="['GET /admin/alibaba/listCybUserGroupFeed']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">同步商品信息</el-button>
      <el-select v-model="listQuery.libraryid" clearable value-key="id" @change="handleCategoryChange" style="width: 200px" class="filter-item" placeholder="请选择类库">
        <el-option  v-for="index in libraryList"
        :key="index.id"
        :label="index.title"
        :value="index.productLibraryId"></el-option>
      </el-select>
       <el-select v-model="listQuery.status" clearable value-key="value" @change="scnystatusSelect" style="width: 200px" class="filter-item" placeholder="选择状态">
        <el-option  v-for="index in scnystatusList"
        :key="index.value"
        :label="index.lable"
        :value="index.value"></el-option>
      </el-select>
     <el-button v-permission="['GET /admin/alibaba/listCybUserGroup']" class="filter-item" type="primary" icon="el-icon-edit" @click="syncProductbatch">批量同步</el-button>

    
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" @selection-change="changeFun" border fit highlight-current-row>
      <el-table-column type="selection">
    </el-table-column>
      <el-table-column align="center" label="商品名称" prop="title" sortable/>

      <el-table-column align="center" label="商品ID" prop="feedId"/>
      <el-table-column align="center" label="价格" prop="price"/>

      <el-table-column align="center" label="imgUrl" prop="imgurl">
        <template slot-scope="scope">
          <img v-if="scope.row.imgurl" :src="scope.row.imgurl" width="80">
        </template>
        
      </el-table-column>

      <el-table-column  label="类目选择">
          <template slot-scope="scope">
             <el-cascader :options="cateGoryList" v-model="scope.categoryId" expand-trigger="hover" @change="rowCategoryChange(scope)"/>
          </template>
        </el-table-column>

      <el-table-column align="center" label="状态" prop="status">
        <template slot-scope="scope">
    <span v-if="scope.row.status == 0">未同步</span> 
    <span v-if="scope.row.status == 1">已同步</span>
    <span v-if="scope.row.status == 2">取消同步</span>
  
  </template>

      </el-table-column>

      <el-table-column align="center" label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-permission="['POST /admin/alibaba/productInfo']" type="primary" size="mini" @click="handleUpdateSelfPrice(scope.row)">特价同步</el-button>
          <!-- <el-button v-permission="['POST /admin/alibaba/productInfo']" type="info" size="mini" @click="handleUpdate(scope.row)">默认同步</el-button> -->
          <el-button v-permission="['POST /admin/alibaba/deleteProduct']" type="danger" size="mini" @click="deleteProduct(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog  :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
       
        
        <el-form-item label="所属分类">
          <el-cascader
            :options="cateGoryList"
            expand-trigger="hover"
            @change="batch2SncyProduct"
          />
        </el-form-item>

      
       <el-form-item label="邮费" prop="franking">
          <el-input v-model="listQuery.franking" />
        </el-form-item>
        
        <el-form-item label="让利出售:" prop="selfPrice">
          <el-radio v-model="listQuery.selfPrice" :label=false>禁用</el-radio>
          <el-radio v-model="listQuery.selfPrice" :label=true>启用</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
      
        <el-button  type="primary" @click="confirmToBatch">确定</el-button>
      </div>
    </el-dialog>

    <div>


    

    </div>

  </div>
</template>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 145px;
  height: 145px;
  display: block;
}
</style>

<script>
import { listCybUserGroup,listCybUserGroupFeed,productInfo ,getListByGroupId,batchSncyProducts,productInfoByCateGoryId,deleteProduct} from '@/api/alibaba'
import { getToken } from '@/utils/auth'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'productInfo',
  components: { Pagination },
  data() {
    return {
     // uploadPath,
      list: null,
      total: 0,
      listLoading: true,
      libraryList:[],
      cateGoryList:[],
      listQuery: {
        page: 1,
        size: 10,
        libraryid: 3784,
        franking:0,
        selfPrice: false,
        offerId:0,
        categoryId:0,
        productslist:null,
        sort: 'add_time',
        status:0,
        order: 'desc'
      },
       checkBoxData: [],
      dataForm: {
        id: undefined,
        username: undefined,
        password: undefined,
        avatar: undefined,
        roleIds: []
      },
      scnystatusList:[{'lable':'未同步','value':0},
                      {'lable':'已同步','value':1}, 
                      {'lable':'已删除','value':2}],
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        username: [
          { required: true, message: '管理员名称不能为空', trigger: 'blur' }
        ],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  computed: {
    headers() {
      return {
        'X-Litemall-Admin-Token': getToken()
      }
    }
  },
  created() {
    this.getList()


  },
  methods: {
    formatRole(roleId) {
      
      return ''
    },
    getList() {
      this.listLoading = true
      
      getListByGroupId(this.listQuery)
        .then(response => {
          
          this.list = response.data.data.productList
          this.libraryList=response.data.data.libraryList
          this.total = response.data.data.totalRow
          this.cateGoryList=response.data.data.cateGoryList.data.categoryList
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
     // this.listQuery.pageNo = 1
     // this.getList()
     this.listLoading = true
     
     listCybUserGroupFeed(this.listQuery)
        .then(response => {
           this.listLoading = false
          this.$notify.success({
          title: '成功',
          message: '同步成功'
        })
         this.getList()
        })
        .catch(() => {
           this.listLoading = false
            this.$notify.success({
          title: '失败',
          message: '同步异常'
        })
         
        })
    },
    syncProductLibrary() {
       this.listLoading = true
       listCybUserGroup(this.listQuery)
        .then(response => {
           this.listLoading = false
          this.$notify.success({
          title: '成功',
          message: '同步成功'
        })
         this.getList();
        })
        .catch(() => {
           this.listLoading = false
            this.$notify.success({
          title: '失败',
          message: '同步异常'
        })
         
        })
      
    },
    resetForm() {
      this.dataForm = {
        id: undefined,
        username: undefined,
        password: undefined,
        avatar: undefined,
        roleIds: []
      }
    },
    uploadAvatar: function(response) {
      this.dataForm.avatar = response.data.url
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleCategoryChange(value) {
     // this.goods.categoryId = value[value.length - 1];
     this.listQuery.libraryid = value;
     this.getList();
    },
    scnystatusSelect(value){
        this.listQuery.status = value
        this.getList();
    },
    handleUpdate(row) {
      this.listLoading = true
      this.listQuery.offerId = row.feedId
        this.listQuery.selfPrice = false
       productInfo(this.listQuery)
        .then(response => {
          this.listLoading = false
          this.$notify.success({
          title: response.data.errormsg,
          message: response.data.data
        })

        })
        .catch(() => {
          this.listLoading = false
          this.$notify.success({
          title: '失败',
          message: '执行异常'
        })
        })
    },

    handleUpdateSelfPrice(row) {
      this.listLoading = true
      this.listQuery.offerId = row.feedId
      this.listQuery.selfPrice = true
       productInfo(this.listQuery)
        .then(response => {
          this.listLoading = false
          this.$notify.success({
          title: response.data.errormsg,
          message: response.data.data
        })

        })
        .catch(() => {
          this.listLoading = false
          this.$notify.success({
          title: '失败',
          message: '执行异常'
        })
        })
    },


 deleteProduct(scope) {
    debugger
    this.list.splice(scope.$index,1)
       deleteProduct(scope.row.id)
        .then(response => {
          this.$notify.success({
          title: "成功",
          message: "删除成功"
        })
        
      this.list.splice
        })
        .catch(() => {
          this.listLoading = false
          this.$notify.success({
          title: '失败',
          message: '执行异常'
        })
        })
    },
   changeFun(val) {
            this.checkBoxData = val;
        },
        //批量同步
    syncProductbatch(){
        this.dialogFormVisible=true
        
    },
    //批量添加分类
    batch2SncyProduct(value){

    this.listQuery.categoryId = value[value.length-1];
    

    },
    //单行商品修改类目
    rowCategoryChange(val){
      var row = val.row
      var categoryId = val.categoryId
      //this.list[val.$index].categoryId = categoryId[categoryId.length-1]
      this.listQuery.offerId=row.feedId
      this.listQuery.categoryId =categoryId[categoryId.length-1]
    productInfoByCateGoryId(this.listQuery).then(response =>{

       this.listLoading=false
          this.$notify.success({
          title: '成功',
          message: "同步成功"
        })
        this.getList()
        }).catch(() => {
           this.listLoading=false
          this.listLoading = false
          this.$notify.success({
          title: '失败',
          message: '执行异常'
        })
        


    })
    },
    confirmToBatch(){
       this.dialogFormVisible=false
      this.listLoading = true
          var tmp=''; 
        for(var i = 0 ;i<this.checkBoxData.length;i++){
            tmp+=this.checkBoxData[i].feedId
            if(i!=this.checkBoxData.length-1)
             tmp+=','
        }
        console.log(tmp)
        this.listQuery.productslist=tmp
        batchSncyProducts(this.listQuery).then(response =>{
          this.listLoading=false
          this.$notify.success({
          title: '成功',
          message: "同步成功"
        })
        this.getList()
        }).catch(() => {
           this.listLoading=false
          this.listLoading = false
          this.$notify.success({
          title: '失败',
          message: '执行异常'
        })
        })
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['管理员ID', '管理员名称', '管理员头像']
        const filterVal = ['id', 'username', 'avatar']
        excel.export_json_to_excel2(
          tHeader,
          this.list,
          filterVal,
          '管理员信息'
        )
        this.downloadLoading = false
      })
    }
  }
}
</script>
